﻿using Microsoft.TeamFoundation.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace CMS6Tools.Management
{
    public static class Util
    {
        public const string DefaultIgnoredFolderCopyPattern = @"\\((bin)|(debug)|(release)|(obj))(|(\\.*))";
        public static TfsTeamProjectCollection AuthenticatedTfsTeamProjectCollection;
        public static readonly string DefaultTFSURL = ConfigurationManager.AppSettings["codeChangeTfsDefaultCollectionURL"] ?? "http://andromeda:8090/tfs/CmsCollection";
        public static readonly string DefaultUserName = ConfigurationManager.AppSettings["codeChangeTfsCredentialDefaultUserName"] ?? "NETVIP\\yuliang";

        public static bool AutoAuthenticate()
        {
            if (AuthenticatedTfsTeamProjectCollection != null)
                return true;

            try
            {
                var credential = CredentialCache.DefaultNetworkCredentials;
                var tfsTeamProjectCollection = new TfsTeamProjectCollection(new Uri(DefaultTFSURL), credential);

                tfsTeamProjectCollection.EnsureAuthenticated();
                if (tfsTeamProjectCollection.HasAuthenticated)
                {
                    AuthenticatedTfsTeamProjectCollection = tfsTeamProjectCollection;
                    return true;
                }
                else
                {
                    AuthenticatedTfsTeamProjectCollection = null;
                    return false;
                }
            }
            catch
            { 
            }
            return false;
        }
    }
}
